﻿using MvvmFx.Common.ViewModels.Behaviors.ChangeTracking;
using MvvmFx.Common.ViewModels.Behaviors.LoadingSaving;

namespace MvvmFx.Common.ViewModels.Behaviors.ProcessManagement
{
    public class StepViewModelBase : ViewModelBase, IProcessStep
    {
        #region Fields

        private readonly IViewModelLoader _loader;
        private readonly IViewModelSaver _saver;
        private readonly ChangeService _changeService;
        private string _key;

        #endregion

        #region Constructor

        public StepViewModelBase(IViewModelLoader loader,
                                 IViewModelSaver saver,
                                 string key)
        {
            _loader = loader;
            _saver = saver;
            _changeService = new ChangeService(this);
            _key = key;
        }

        #endregion

        #region IProcessStep Members

        /// <summary>
        /// Gets or sets the value of the <see cref=”Key”/> property.
        /// </summary>
        public string Key
        {
            get { return _key; }
            set
            {
                if (_key != value)
                {
                    _key = value;
                    OnPropertyChanged("Key");
                }
            }
        }

        public void Load()
        {
            _loader.Load(this);
        }

        /// <summary>
        /// Saves this page
        /// </summary>
        public void Save()
        {
            if (_saver != null)
            {
                _saver.Save(this);
            }
        }

        public bool CanLeave()
        {
            return true;
        }

        public ChangeService ChangeService
        {
            get { return _changeService; }
        }

        #endregion

    }
}